package com.tanhua.util;

import com.tanhua.sso.pojo.Friend;
import com.tanhua.sso.pojo.Movement;
import com.tanhua.sso.pojo.MovementTimeLine;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class TimeLineService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //多线程异步执行
    @Async
    public void saveTimeLine(Long userId, ObjectId id){
        Criteria criteria = Criteria.where("userId").is(userId);
        //好友关系表
        List<Friend> friends = mongoTemplate.find(Query.query(criteria), Friend.class);
        for (Friend friend : friends) {
            MovementTimeLine movementTimeLine=new MovementTimeLine();
            //设置动态id
            movementTimeLine.setMovementId(id);
            //设置当前时间
            movementTimeLine.setCreated(System.currentTimeMillis());
            //设置好友id
            movementTimeLine.setFriendId(friend.getFriendId());
            //设施当前有用户id
            movementTimeLine.setUserId(userId);
            //保存到数据库
            mongoTemplate.save(movementTimeLine);
        }
    }
}
